package com.example.myflutter;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.android.FlutterFragment;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    FlutterView flutterView;
    FlutterEngine flutterEngine;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //view级别引入
        showFlutterView();

         //fragment级别引入
//        showFlutterFragment();
        //activity级别引入
//        showFlutterActivity();


        setListener();



    }


    private void showFlutterView() {

        flutterEngine = new FlutterEngine(this);
        flutterEngine.getDartExecutor().executeDartEntrypoint(
                DartExecutor.DartEntrypoint.createDefault()
        );
//        跳转到指定页面  router1?{\"name\":\"StephenCurry\"}
        flutterEngine.getNavigationChannel().setInitialRoute("router1");



        // 通过FlutterView引入Flutter编写的页面
        flutterView = new FlutterView(this);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        FrameLayout flContainer = findViewById(R.id.frameLayout);
        flContainer.addView(flutterView, lp);
        // 关键代码，将Flutter页面显示到FlutterView中
        flutterView.attachToFlutterEngine(flutterEngine);
    }
    private void showFlutterFragment() {
//        FlutterFragment flutterFragment = FlutterFragment.createDefault();
        //跳转到指定页面  router1?{\"name\":\"StephenCurry\"}
        FlutterFragment flutterFragment = FlutterFragment.withNewEngine()
                .initialRoute("router1")
                .build();
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.frameLayout, flutterFragment)
                .commit();
    }

    private void showFlutterActivity() {
        textView = findViewById(R.id.tv_word);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到默认页面
//                startActivity(FlutterActivity.createDefaultIntent(MainActivity.this));
//                跳转到指定页面  router1?{\"name\":\"StephenCurry\"}
                startActivity(
                        FlutterActivity
                                .withNewEngine()
                                .initialRoute("router1")
                                .build(MainActivity.this)
                );
            }
        });
    }

    private void setListener() {
        MethodChannel nativeChannel = new MethodChannel(flutterEngine
                .getDartExecutor(), "com.dlfc");
        nativeChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
                switch (methodCall.method) {
                    case "jumpToNative":
                        // 跳转原生页面
                        Log.d("TAG", "onMethodCall: "+methodCall.argument("name"));
//                        textView.setText(methodCall.argument("name"));
//                        flutterView.setVisibility(View.GONE);
                        break;
                    default:
                        result.notImplemented();
                        break;
                }
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        flutterEngine.getLifecycleChannel().appIsResumed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        flutterEngine.getLifecycleChannel().appIsInactive();
    }

    @Override
    protected void onStop() {
        super.onStop();
        flutterEngine.getLifecycleChannel().appIsPaused();
    }

}